package com.yanyu.space.common.sleuth.web;

import brave.http.HttpRequest;
import brave.sampler.SamplerFunction;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpMethod;

import java.util.List;

@Slf4j
@AllArgsConstructor
public class SkipSamplerPredicaterHandler implements SamplerFunction<HttpRequest> {

    private List<SamplerPredicater> predicaters;

    @Override
    public Boolean trySample(HttpRequest request) {
        if (HttpMethod.OPTIONS.matches(request.method())) {
            return false;
        }
        if (CollectionUtils.isNotEmpty(predicaters)) {
            for (SamplerPredicater predicater : predicaters) {
                if (!predicater.trySample(request)) {
                    return false;
                }
            }
        }
        return null;
    }
}
